package com.it.reggie.controller.Interceptors;

import com.alibaba.fastjson.JSON;
import com.it.reggie.commen.BaseContext;
import com.it.reggie.commen.R;
import com.it.reggie.entity.Employee;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component

//配置拦截器
public class ProjectInterceptors implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession();
        Employee emp = (Employee) session.getAttribute("id");

        if (emp==null){

            response.sendRedirect("http://localhost:8080/backend/page/login/login.html");

            return false;

        }else {
            Long id = emp.getId();
            BaseContext.setCurrentId(id);
            return true;
        }

    }
}
